package me.sdevil507.org.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.common.exceptions.DeptCodeExistedException;
import me.sdevil507.biz.common.exceptions.DeptTitleExistedException;
import me.sdevil507.org.dto.OrgDeptMergeParamsDto;
import me.sdevil507.org.dto.OrgDeptParamsDto;
import me.sdevil507.org.dto.OrgDeptResultDto;
import me.sdevil507.org.dto.OrgDeptSimpleResultDto;
import me.sdevil507.org.mapper.OrgDeptMapper;
import me.sdevil507.org.po.OrgDeptPo;
import me.sdevil507.org.service.OrgDeptService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 机构部门表[OrgDeptController]
 *
 * @author sdevil507
 * created on 2020-06-24 14:35:36
 */
@Controller
@RequestMapping("/api/org/dept")
@Slf4j
public class OrgDeptController {

    @Autowired
    private OrgDeptService orgDeptService;

    @Autowired
    private OrgDeptMapper orgDeptMapper;

    /**
     * 获取全部信息列表
     *
     * @param dto 聚合查询/分页/排序参数
     * @return 信息列表
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAll", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO readAll(@RequestBody OrgDeptMergeParamsDto dto) {
        List<OrgDeptResultDto> list = orgDeptMapper.pos2dtos(orgDeptService.readAll(dto.getSearchDto()));
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list);
    }

    /**
     * 获取全部信息列表
     *
     * @param dto 聚合查询/分页/排序参数
     * @return 信息列表
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAllByPage", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"org:dept:view","plat:dept:view"}, logical= Logical.OR)
    public ApiResultDTO readAllByPage(@RequestBody OrgDeptMergeParamsDto dto) {
        Page<OrgDeptPo> pagePos = orgDeptService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<OrgDeptSimpleResultDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<OrgDeptSimpleResultDto, OrgDeptPo>() {
            @Override
            public List<OrgDeptSimpleResultDto> exec(List<OrgDeptPo> list) {
                return orgDeptMapper.pos2SimpleDtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 创建
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"org:dept:add","plat:dept:add"}, logical= Logical.OR)
    public ApiResultDTO create(@RequestBody OrgDeptParamsDto dto) {
        try {
            orgDeptService.create(dto);
        } catch (DeptTitleExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.DEPT_TITLE_EXISTED.getCode(), ApiStatusCode.DEPT_TITLE_EXISTED.getDescription());
        } catch (DeptCodeExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.DEPT_CODE_EXISTED.getCode(), ApiStatusCode.DEPT_CODE_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 更新
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"org:dept:edit","plat:dept:edit"}, logical= Logical.OR)
    public ApiResultDTO update(@RequestBody OrgDeptParamsDto dto) {
        try {
            orgDeptService.update(dto);
        } catch (DeptTitleExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.DEPT_TITLE_EXISTED.getCode(), ApiStatusCode.DEPT_TITLE_EXISTED.getDescription());
        } catch (DeptCodeExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.DEPT_CODE_EXISTED.getCode(), ApiStatusCode.DEPT_CODE_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"org:dept:delete","plat:dept:delete"}, logical= Logical.OR)
    public ApiResultDTO delete(@RequestBody OrgDeptParamsDto dto) {
        orgDeptService.delete(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }
}